<?php
namespace app\portal\controller;
use cmf\controller\AdminBaseController;
use think\Db;

class AdminAboutController extends AdminBaseController
{

  public function index() {
    $data = Db::name('option')->where('option_name', 'about_us')->find();
    if (empty($data)) {
      $data = [
        'add' => 1,
        'content' => '',
        'picture' => '',
      ];
    } else {
      $data = json_decode($data['option_value'], true);
      $data['content'] = htmlspecialchars_decode($data['content']);
      $data['add'] = 0;
    }

    $this->assign('data', $data);

    return $this->fetch();
  }

  public function save() {
    if (!$this->request->isPost()) {
      return $this->error('Forbidden', 403);
    }

    $param = $this->request->param();
    $post = $param['post'];

    $data = [
      'picture' => $post['thumb'],
      'content' => $post['content'],
    ];
    $data = json_encode($data);

    if ($post['add']) {
      // 新增
      Db::name('option')->insert(['autoload' => '1', 'option_name' => 'about_us', 'option_value' => $data]);
    } else {
      // 更新
      Db::name('option')->where('option_name', 'about_us')->update(['option_value' => $data]);
    }

    $this->success('保存成功！');
  }

}
